<?php
/*
 * @todo    团购模型
 * @author  fang
 */
namespace Admin\Model;
use Admin\Model\BaseModel;

class ShopGroupModel extends BaseModel{
    
    /* 自动验证规则 */
    protected $_validate = array(
        array('shop_group_price', 'require', '团购价格必须填写', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('shop_group_price', '0', '团购价格必须是大于0的数字', self::EXISTS_VALIDATE, 'notequal', self::MODEL_BOTH),
        array('shop_group_customer_number', 'require', '团购人数必须填写', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('shop_group_customer_number', '0', '团购人数必须是大于0的数字', self::EXISTS_VALIDATE, 'notequal', self::MODEL_BOTH),
        array('shop_group_stock', 'require', '团购库存必须填写', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('shop_group_stock', '0', '团购库存必须是大于0的数字', self::MUST_VALIDATE, 'notequal', self::MODEL_BOTH),
        //array('pic_id', 'gtNumber', '请上传图片', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    );
    
    /* 返回主键 */
    public function getPk(){
        return 'shop_group_id';
    }
    
    /* 自动完成规则 */
    protected $_auto = array(
        array('create_time', 'time', self::MODEL_INSERT, 'function'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
    );
    
    /**
     * @todo    获取商品信息
     * @param   array     $map      条件
     * @param   string    field     字段
     * @author  fang
     */
    public function getGoodsInfo($map, $field = true){
        if(empty($map)){
            $this->error = '条件参数错误';
            return null;
        }
        $goods_info = M('shop_goods')->field($field)->where($map)->find();
        if(!$goods_info){
            $this->error = '商品信息错误';
            return null;
        }
        $goods_info['merchant_name'] = (isset($goods_info['merchant_id']))? getMerchantName($goods_info['merchant_id']) : '' ;
        $goods_info['shop_name'] = (isset($goods_info['shop_id']))? getShopName($goods_info['shop_id']) : '' ;
        $goods_info['goods_category_pid_name'] = (isset($goods_info['goods_category_pid']))? getGoodsCategoryName($goods_info['goods_category_pid']) : '' ;
        $goods_info['goods_category_id_name'] = (isset($goods_info['goods_category_id']))? getGoodsCategoryName($goods_info['goods_category_id']): '' ;
        return $goods_info;
    }
    
    /**
     * @todo    获取商品团购信息
     * @param   array     $map      条件
     * @param   string    field     字段
     * @author  fang
     */
    public function getGroupGoodsInfo($map, $field = true){
        if(empty($map)){
            $this->error = '条件参数错误';
            return null;
        }
        $group_info = M('shop_group')->field($field)->where($map)->find();
        if($group_info){
            return $group_info;
        }else{
            $this->error = '团购信息错误';
            return null;
        }
    }
    
    /**
     * @todo    商品类型状态修改
     * @param   int     goods_id    商品id
     * @param   int     type        类型（0:正常;1:促销;2:团购）
     * @author  fang
     */
    public function setShopGoodsStatus($goods_id, $type = 0){
        $goods_id = (int)$goods_id;
        $type = (int)$type;
        if($goods_id == 0){
            $this->error = '参数错误';
        }
        $where['goods_id'] = $goods_id;
        return M('shop_goods')->where($where)->setField('goods_type', $type);
    }

    /*
     * @todo    取消商品团购
     * @param   int/array   $ids    商品id
     * @author  fang
     */
    public function delGroup($ids){
        if(empty($ids)){
            $this->error = '请选择删除数据';
            return false;
        }
        //获取团购商品ids
        $where['shop_group_id'] = array('in', $ids);
        $goods_list = M('shop_group')->field('goods_id')->where($where)->select();
        $map = array();
        if($goods_list){
            $map['goods_type'] = 2;
            $map['goods_id'] = array('in', array_column($goods_list, 'goods_id'));
        }
        //取消团购
        $edit_type = true;
        $this->startTrans();
        //重置商品状态
        if($map){
            $edit_type = M('shop_goods')->where($map)->setField('goods_type', 0);
        }
        //删除团购信息
        $del_group = M('shop_group')->where($where)->delete();
        if($edit_type && $del_group){
            $this->commit();
            //删除团购日志
            $del_id = (is_array($ids))? UID : $ids ;
            action_log('delGroup', 'shopGroup', $del_id, UID);
            return true;
        }else{
            $this->rollback();
            $this->error = '删除团购数据失败';
            return false;
        }
        
    }
}